<template>
  <div class="community-building-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button v-if="level" type="success" @click="backUpLevel">
          返回上一级
        </el-button>
        <el-button v-else type="success" @click="backUpLevel">
          返回园区列表
        </el-button>
        <el-button :icon="Plus" type="primary" @click="handleEdit('', level)">
          添加
        </el-button>
        <!-- <el-button :icon="Delete" type="danger" @click="handleDelete">
          批量删除
        </el-button> -->
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              placeholder="请输入标题"
            />
          </el-form-item>
          <el-form-item>
            <el-button :icon="Search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <!-- 楼幢管理 -->
    <el-table
      v-if="level == 0"
      v-loading="listLoading"
      :data="BuildList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="BuildID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="楼幢名">
        <template #default="{ row }">
          <el-button text @click="tonextLevel(row, 1)">
            {{ row.BuildName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="展示" width="100">
        <template #default="{}">
          <el-button type="primary" @click="handleShow">展示</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 0)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 楼幢单元列表 -->
    <el-table
      v-if="level == 1"
      v-loading="listLoading"
      :data="BuildUnitList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="BuildUnitID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="时间"
        prop="addTime"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="楼幢单元">
        <template #default="{ row }">
          <el-button text @click="tonextLevel(row, 2)">
            {{ row.BuildUnit }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 1)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 楼层 -->
    <el-table
      v-if="level == 2"
      v-loading="listLoading"
      :data="FloorList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="FloorID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="时间"
        prop="addTime"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="楼层">
        <template #default="{ row }">
          <el-button text @click="tonextLevel(row, 3)">
            {{ row.FloorName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 2)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 房间号 -->
    <el-table
      v-if="level == 3"
      v-loading="listLoading"
      :data="RoomList"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="id"
        prop="RoomID"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="园区"
        prop="CommunityName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="楼幢"
        prop="BuildName"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="单元"
        prop="BuildUnit"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        label="楼层"
        prop="FloorName"
        show-overflow-tooltip
      />
      <el-table-column align="center" label="房间号">
        <template #default="{ row }">
          <el-button text>
            {{ row.RoomName }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" label="是否开启" width="100">
        <template #default="{ row }">
          <el-switch
            v-model="row.state"
            :inactive-value="0"
            :active-value="1"
            @change="onChange(row)"
          />
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" width="200">
        <template #default="{ row }">
          <el-button text @click="handleEdit(row, 3)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="isFetchData" />
  </div>
</template>

<script>
  import {
    doDelete,
    buildingList,
    BuildUnitList,
    floorList,
    roomList,
  } from '@/api/community'
  import Edit from './components/CommunityBuildingEdit'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'CommunityBuilding',
    components: { Edit },
    setup() {
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')
      const router = useRouter()
      const route = useRoute()
      const state = reactive({
        RoomList: [],
        FloorList: [],
        BuildUnitList: [],
        BuildList: [],
        level: 0,
        editRef: null,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        CommunityID: 0,
        BuildID: 0,
        BuildUnitID: 0,
        FloorID: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          title: '',
        },
      })

      // 进入下一级别
      const tonextLevel = (row, num) => {
        state.level = num
        // 楼幢单元 接口
        if (num == 1) {
          state.BuildID = row.BuildID
          getBuildUnitList()
        }
        // 楼层 接口
        if (num == 2) {
          state.BuildUnitID = row.BuildUnitID
          getFloorList()
        }
        // 房间号 接口
        if (num == 3) {
          state.FloorID = row.FloorID
          getRoomList()
        }
        console.log(row, num)
      }

      const handleShow = () => {
        router.push({
          path: '/community/building/buildingShow',
          query: {
            CommunityID: state.CommunityID,
          },
        })
      }

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const handleEdit = (row, num) => {
        console.log(num, row, '222')

        if (num == 0) {
          if (row.BuildID) {
            state.editRef.showEdit(row, num)
          } else {
            const Msg = {
              BuildID: 0,
            }
            state.editRef.showEdit(Msg, num)
          }
        }
        if (num == 1) {
          if (row.BuildUnitID) {
            state.editRef.showEdit(row, num)
          } else {
            const Msg = {
              BuildUnitID: 0,
            }
            state.editRef.showEdit(Msg, num)
          }
        }
        if (num == 2) {
          if (row.FloorID) {
            state.editRef.showEdit(row, num)
          } else {
            const Msg = {
              FloorID: 0,
            }
            state.editRef.showEdit(Msg, num)
          }
        }
        if (num == 3) {
          if (row.BuildUnitID) {
            state.editRef.showEdit(row, num)
          } else {
            const Msg = {
              BuildID: state.BuildID,
              BuildUnitID: state.BuildUnitID,
              FloorID: state.FloorID,
              RoomID: 0,
            }
            state.editRef.showEdit(Msg, num)
          }
        }
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      // const handleSizeChange = (val) => {
      //   state.queryForm.pageSize = val
      //   fetchData()
      // }
      // const handleCurrentChange = (val) => {
      //   state.queryForm.pageNo = val
      //   fetchData()
      // }
      // const queryData = () => {
      //   state.queryForm.pageNo = 1
      //   fetchData()
      // }
      const isFetchData = () => {
        if (state.level == 0) {
          getBuildingList()
        }
        if (state.level == 1) {
          getBuildUnitList()
        }
        if (state.level == 2) {
          getFloorList()
        }
        if (state.level == 3) {
          getRoomList()
        }
      }

      onMounted(() => {
        if (route.query.CommunityID) {
          state.CommunityID = route.query.CommunityID
        }
        getBuildingList()
      })

      watch(route, (newVal, oldValue) => {
        console.log(newVal, oldValue, '12312312')
        if (newVal.query.CommunityID) {
          state.CommunityID = route.query.CommunityID
        }
        getBuildingList()
      })

      const getBuildingList = async () => {
        state.listLoading = true
        state.level = 0
        const dataMsg = {
          CommunityID: state.CommunityID,
        }
        const {
          data: { list, total },
        } = await buildingList(dataMsg)
        state.BuildList = list
        state.total = total
        state.listLoading = false
        console.log(list, '1111')
      }
      const getBuildUnitList = async () => {
        const dataMsg = {
          BuildID: state.BuildID,
        }
        const {
          data: { list, total },
        } = await BuildUnitList(dataMsg)
        state.BuildUnitList = list
        state.total = total

        console.log(list, '2222')
      }
      const getFloorList = async () => {
        const dataMsg = {
          BuildUnitID: state.BuildUnitID,
        }
        const {
          data: { list, total },
        } = await floorList(dataMsg)
        state.FloorList = list
        state.total = total
        console.log(list, '3333')
      }

      const getRoomList = async () => {
        const dataMsg = {
          FloorID: state.FloorID,
          BuildUnitID: state.BuildUnitID,
          BuildID: state.BuildID,
        }
        const {
          data: { list, total },
        } = await roomList(dataMsg)
        state.RoomList = list
        state.total = total
        console.log(list, '4444')
      }

      // 返回上一级
      const backUpLevel = () => {
        if (state.level != 0) {
          state.level -= 1
        } else {
          router.push({
            path: '/community/communityIndex',
          })
        }

        console.log(state.level, ' state.level')
      }

      return {
        ...toRefs(state),
        setSelectRows,
        backUpLevel,
        getBuildingList,
        getBuildUnitList,
        getFloorList,
        getRoomList,
        tonextLevel,
        handleShow,
        handleEdit,
        isFetchData,
        handleDelete,
        // handleSizeChange,
        // handleCurrentChange,
        // queryData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>
